/**
 * AdminAccountDAO.java
 * Nov 7, 2013
 * Copyright: phongtdse60732@fpt.edu.vn
 */
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import DBUltilities.ConnectDB;

/**
 * @author trandophong
 * @version 1.0
 */
public class AdminAccountDAO {
	public void add() {
		String uname = "phongtd";
		String password = "phongtd";
		String email = "phongtd";
		String name = "name";

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "INSERT INTO ADMINACCOUNT"
				+ "(USERNAME, PASSWORD, EMAIL, NAME) VALUES" + "(?,?,?, ?)";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, uname);
			preparedStatement.setString(2, password);
			preparedStatement.setString(3, email);
			preparedStatement.setString(4, name);

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
	}

	public void update() {
		String uname = "phongtd";
		String password = "phongtdHKT";
		String email = "phongtdOOO";
		String name = "phongtdOOO";

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "UPDATE ADMINACCOUNT SET PASSWORD = ?, EMAIL = ?, NAME = ? WHERE USERNAME = ?";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(4, uname);
			preparedStatement.setString(1, password);
			preparedStatement.setString(2, email);
			preparedStatement.setString(3, name);

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
	}

	public void delete() {
		String uname = "phongtd";

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "DELETE FROM ADMINACCOUNT WHERE USERNAME = ?";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, uname);

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
	}

	public boolean checkAuthenticate(String username, String password) {
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "SELECT * FROM ADMINACCOUNT WHERE USERNAME = ? AND PASSWORD = ?";
		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, username);
			preparedStatement.setString(2, DBUltilities.CommonUtilities.MD5(password));

			return preparedStatement.executeQuery().next();

		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
		return false;
	}
}
